home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / ams__l~1.zoo / src / joystick.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-05  |  2.1 KB  |  100 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari Machine Specific Library,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknowledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  described in the file COPYING.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11.  
  12. #include "Joystick.h"
  13. #include "MousePosition.h"
  14. #include "JoyISR.h"
  15. #include <osbind.h>
  16.  
  17. // TERMINATION code:
  18.  
  19. // used to ensure MyJoystickRestorer is linked in if possibly necessary
  20. #define EnsureRestoration { volatile JoystickRestorer* x=&MyJoystickRestorer; }
  21.  
  22. const char JoyJoyString[]="\024";
  23. const char MouseJoyString[]="\010";
  24.  
  25. class JoystickRestorer
  26. {
  27. public:
  28.     JoystickRestorer() {
  29.         _KBDVECS * KV=Kbdvbase();
  30.         OrigJoy=KV->joyvec;
  31.     }
  32.     ~JoystickRestorer() {
  33.         _KBDVECS * KV=Kbdvbase();
  34.         KV->joyvec=OrigJoy;
  35.         Ikbdws(0,MouseJoyString);
  36.     }
  37. private:
  38.     void (*OrigJoy)(void*);
  39. };
  40.  
  41. static JoystickRestorer MyJoystickRestorer;
  42.  
  43.  
  44.  
  45. /* GLOBAL */ volatile int JoyFlags[2]={0,0};
  46.  
  47. int XDif[16] = { 0,0,0,0,-1,-1,-1,-1,1,1,1,1,0,0,0,0 };
  48. int YDif[16] = { 0,-1,1,0,0,-1,1,0,0,-1,1,0,0,-1,1,0 };
  49.  
  50. static int Enabled[2];
  51. static void (*OldJoy)(void*);
  52. static bool joyusemouse=TRUE;
  53.  
  54. Joystick::Joystick(int Port=1) : port(Port)
  55. {
  56.     EnsureRestoration;
  57.  
  58.     if (port==0 || port==1) {
  59.         if (!Enabled[port]) {
  60.             if (!Enabled[1-port]) {
  61.                 // Very first!
  62.                 _KBDVECS * KV=Kbdvbase();
  63.                 OldJoy=KV->joyvec;
  64.                 KV->joyvec=JoyISR;
  65.             }
  66.             if (port==0) {
  67.                 Ikbdws(0,JoyJoyString);
  68.                 joyusemouse=FALSE;
  69.             }
  70.         }
  71.  
  72.         Enabled[port]++;
  73.         Flags=&JoyFlags[port];
  74.     } else {
  75.         // What shall we do with the drunken sailor?
  76.     }
  77. }
  78.  
  79. Joystick::~Joystick()
  80. {
  81.     Enabled[port]--;
  82.  
  83.     if (!Enabled[port]) {
  84.         if (!Enabled[1-port]) {
  85.             // Very last!
  86.             _KBDVECS * KV=Kbdvbase();
  87.             KV->joyvec=OldJoy;
  88.         }
  89.         if (port==0) {
  90.             Ikbdws(0,MouseJoyString);
  91.             joyusemouse=TRUE;
  92.         }
  93.     }
  94. }
  95.  
  96. bool Joystick::Trigger()
  97. {
  98.      return (port && joyusemouse) ? Mouse.RightButton() : bool(!!(*Flags&128));
  99. }
  100.